package leetcode;

import java.util.Random;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/13 20:35
 * @描述 暂无
 */
public class Solution0470 {
    /**
     * The rand7() API is already defined in the parent class SolBase.
     * public int rand7();
     *
     * @return a random integer in the range 1 to 7
     */

    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            System.out.println(rand10());
            i = i + 1;
        }
    }


    /**
     *  正确答案，还可以奥
     * @return
     */
    public static int rand10true() {

        while (true) {
            int num = (rand7() - 1) * 7 + rand7();
            // 如果在40以内，那就直接返回
            if (num <= 40) {
                return 1 + num % 10;
            }
            // 说明刚才生成的在41-49之间，利用随机数再操作一遍
            num = (num - 40 - 1) * 7 + rand7();
            if (num <= 60) {
                return 1 + num % 10;
            }
            // 说明刚才生成的在61-63之间，利用随机数再操作一遍
            num = (num - 60 - 1) * 7 + rand7();
            if (num <= 20) {
                return 1 + num % 10;
            }

        }

    }

    public static int rand10() {
        int a = rand7(), b = rand7();
        while (a > 5)
            a = rand7();
        while (b > 2)
            b = rand7();
        return a * b;
    }

    public static int rand7() {
        Random r = new Random(7);

        return (int) r.nextInt() + 1;
    }
}
